package com.foreknow.demo6;

/**
 * Java常用的工具类String
 * String:表示字符串常量
 * == 如果在字符串中进行比较，比较的是内存地址
 * 如果想要比较两个字符串的内容是否相同，就要使用equals
 */
public class StringUtilDemo {
    public static void main(String[] args) {
        //        String str = "tom123"+"fender";
        //        System.out.println(str);
        String str = "tom".concat("fender").concat("abc").concat("def");
        System.out.println(str);
        System.out.println("-------------------------------------------");
        String s = "aa bb cc";
        //拆分字符串split
        String[] es = s.split(" ");
        for (int i = 0;i<es.length;i++){
            System.out.println(es[i]);
        }
        System.out.println("--------------------==与equals的区别-----------------------");
        String s1 = "haha";
        String s2 = "haha";
        String s3 = new String("haha");
        String s4 = new String("haha");
        String s5 = "ha"+"ha";
        String s6 = new String("ha")+"ha";

        System.out.println(s1 == s2); //true
        System.out.println(s1 == s3); //false
        System.out.println(s1 == s5); //true
        System.out.println(s1 == s6); //false

        System.out.println(s3 == s4); //false
        System.out.println(s3.equals(s4)); //true

        System.out.println("--------------------------------------------");
        //如果想动态构造字符串，可以使用StringBuffer,主要的用途就是在原有字符串基础上拼接字符串
        StringBuffer stringBuffer = new StringBuffer("select * from user where username = ?");
        User user = new User();
        if(user.getPassword()!=null) {
            stringBuffer.append(" and password = ?");
        }
        System.out.println(stringBuffer);

    }
}
